package com.approval.approvalapp.index.dbsx.tshf;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.TextView;

import com.approval.approvalapp.R;
import com.approval.approvalapp.index.dbsx.tshf.fragment.TsHfDbFragment;
import com.approval.approvalapp.index.dbsx.tshf.fragment.TsHfYbFragment;
import com.approval.approvalapp.util.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

//普通办理
public class TsHfActivity extends BaseActivity {
    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.tablayout)
    TabLayout tablayout;
    @Bind(R.id.vp)
    ViewPager vp;
    private String[] titles = {"待办", "已办"};
    private List<Fragment> fragments;

    @Override
    public int getLayoutId() {
        return R.layout.activity_ts_hf;
    }

    @Override
    public void initView() {
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = this.getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            this.getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

        fragments = new ArrayList<>();
        fragments.add(new TsHfDbFragment());
        fragments.add(new TsHfYbFragment());

        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });
        tablayout.setupWithViewPager(vp);

        for (int i = 0; i < fragments.size(); i++) {
            //获得第i个位置的标签的视图
            View tabView = getTabView(i, this);
            //获得第i位置的Tab 对象
            TabLayout.Tab tab = tablayout.getTabAt(i);
            tab.setCustomView(tabView);
        }
    }

    @Override
    public void initData() {

    }

    //刷新tab的字符串
    public void refreshTab0(String num) {
        View tabView = getTabView(num, 0, this);
        TabLayout.Tab tab = tablayout.getTabAt(0);
        View customView = tab.getCustomView();
        if (customView != null) {
            ViewParent parent = customView.getParent();
            if (parent != null) {
                ((ViewGroup) parent).removeView(customView);
            }
        }
        tab.setCustomView(tabView);
    }

    public void refreshTab1(String num) {
        View tabView = getTabView(num, 1, this);
        TabLayout.Tab tab = tablayout.getTabAt(1);
        View customView = tab.getCustomView();
        if (customView != null) {
            ViewParent parent = customView.getParent();
            if (parent != null) {
                ((ViewGroup) parent).removeView(customView);
            }
        }
        tab.setCustomView(tabView);
    }

    public View getTabView(int index, Context context) {
        //通过LayoutInflater类的静态方法 from 得到一个LayoutInflater 对象
        LayoutInflater inflater = LayoutInflater.from(context);
        //通过inflater对象的inflate方法 将布局xml注入成一个view对象
        View tabView = inflater.inflate(R.layout.tabview, null);
        //通过tabView对象,找到视图中的TextView
        TextView titleTv = (TextView) tabView.findViewById(R.id.tv);
        //设置标题
        titleTv.setText(titles[index]);
        return tabView;
    }

    public View getTabView(String count, int index, Context context) {
        //通过LayoutInflater类的静态方法 from 得到一个LayoutInflater 对象
        LayoutInflater inflater = LayoutInflater.from(context);
        //通过inflater对象的inflate方法 将布局xml注入成一个view对象
        View tabView = inflater.inflate(R.layout.tabview, null);
        //通过tabView对象,找到视图中的TextView
        TextView titleTv = (TextView) tabView.findViewById(R.id.tv);
        //设置标题
        if (index == 0) {
            titleTv.setText(titles[index] + "(" + count + ")");
        } else {
            titleTv.setText(titles[index] + "(" + count + ")");
        }
        return tabView;
    }

    @OnClick(R.id.back_iv)
    public void onViewClicked() {
        finish();
    }
}
